package com.briup.jz.web.controller;

import com.briup.jz.bean.Address;
import com.briup.jz.bean.extend.AddressExtend;
import com.briup.jz.service.IAddressService;
import com.briup.jz.utils.Message;
import com.briup.jz.utils.MessageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("address")
@Api(description="地址相关接口")
public class AddressController {
   @Autowired
    private IAddressService iAddressService;
    @ApiOperation("新建或修改地址信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",paramType = "form"),
            @ApiImplicitParam(name = "telephone",value = "电话",paramType = "form"),
            @ApiImplicitParam(name = "province",value = "省份",paramType = "form"),
            @ApiImplicitParam(name = "city",value = "城市",paramType = "form"),
            @ApiImplicitParam(name = "area",value = "地域",paramType = "form"),
            @ApiImplicitParam(name = "address",value = "地址",paramType = "form"),
            @ApiImplicitParam(name = "isDefault",value = "是否默认",paramType = "form"),
            @ApiImplicitParam(name = "userId",value = "用户id",paramType = "form")
    })
    @PostMapping("/saveorupdate")
    public Message saveOrUpdate(Address address){
        iAddressService.saveOrUpdate(address);
        return MessageUtil.success("操作成功");
    }
//根据用户id查询 或者查询所有
@ApiOperation("根据用户id查询地址信息")
  @ApiImplicitParam(name = "id",value = "用户id",paramType = "query")
    @GetMapping("query")
    public  Message query(Long id){
        List<AddressExtend> addresses = iAddressService.queryWithUser(id);
        return  MessageUtil.success(addresses);
    }

    @ApiOperation("通过id删除")
   @GetMapping("/delete")
    public Message delete(Long id) {
        iAddressService.deleteById( id);
        return  MessageUtil.success("操作成功");
    }
}
